import moment from 'dayjs';
import {StringUtils} from "@/utils/StringUtils";
import dayjs from "dayjs";

export class Calender {
  constructor() {
    this.start = '';
    this.end = '';
    this.format = 'YYYY/MM/DD'
    this.dayS = ' 00:00'
    this.dayE = ' 23:59'
  }
  /**
   * 计算两个日期时间相差的年数、月数、天数、小时数、分钟数、秒数
   * DIFFTIME(开始时间,结束时间,[单位])，单位可以是 "y" 、"M"、"d"、"h"、"m"、"s"'
   * console.log(DIFFTIME('2019-6-30 13:20:00', '2020-10-01 11:20:32', 's'))
   */
  static DIFFTIME  (startTime, endTime, unit) {
    // 判断当前月天数
    function getDays(mouth, year) {
      let days = 30;
      if (mouth === 2) {
        days = year % 4 === 0 ? 29 : 28;
      } else if (mouth === 1 || mouth === 3 || mouth === 5 || mouth === 7 || mouth === 8 || mouth === 10 || mouth === 12) {
        // 月份为：1,3,5,7,8,10,12 时，为大月.则天数为31；
        days = 31;
      }
      return days;
    }

    const start = new Date(startTime);
    const end = new Date(endTime);
    // 计算时间戳的差
    const diffValue = end - start;
    // 获取年
    const startYear = start.getFullYear();
    const endYear = end.getFullYear();
    // 获取月
    const startMouth = start.getMonth() + 1;
    const endMouth = end.getMonth() + 1;
    // 获取日
    const startDay = start.getDate();
    const endDay = end.getDate();
    // 获取小时
    const startHours = start.getHours();
    const endHours = end.getHours();
    // 获取分
    const startMinutes = start.getMinutes();
    const endMinutes = end.getMinutes();
    // 获取秒
    const startSeconds = start.getSeconds();
    const endSeconds = end.getSeconds();
    // 下方注释两行为调试用
    // console.log('start:', startYear, startMouth, startDay, startHours, startMinutes, startSeconds)
    // console.log('end:', endYear, endMouth, endDay, endHours, endMinutes, endSeconds)
    if (unit === 'y' || unit === 'M') {
      // 相差年份月份
      const diffYear = endYear - startYear;
      // 获取当前月天数
      const startDays = getDays(startMouth, startYear);
      const endDays = getDays(endMouth, endYear);
      const diffStartMouth = (startDays - (startDay + ((startHours * 60 + startMinutes + startSeconds / 60) / 60 / 24) - 1)) / startDays;
      const diffEndMouth = (endDay + ((endHours * 60 + endMinutes + endSeconds / 60) / 60 / 24) - 1) / endDays;
      const diffMouth = diffStartMouth + diffEndMouth + (12 - startMouth - 1) + endMouth + (diffYear - 1) * 12;
      if (unit === 'y') {
        return Math.floor(diffMouth / 12 * 100) / 100;
      } else {
        return diffMouth;
      }
    } else if (unit === 'd') {
      return parseInt(diffValue / 1000 / 60 / 60 / 24);
    } else if (unit === 'h' || unit === 'hour' || unit === 'hours') {
      return parseInt(diffValue / 1000 / 60 / 60);
    } else if (unit === 'm' || unit === 'minutes' || unit === 'minute') {
      return parseInt(diffValue / 1000 / 60);
    } else if (unit === 's') {
      return parseInt(diffValue / 1000);
    } else {
      // console.log('请输入正确的单位')
    }
  }

  static formatDate(date,format = 'YYYY-MM-DD'){
    if(StringUtils.isEmptyOrNull(date) || !dayjs(date).isValid()){
      return ''
    }
    let res = dayjs(date).format(format)
    return res
  }

  today() {
    let d = moment();

    return [d.format(this.format + this.dayS), d.format(this.format + this.dayE)]
  }

  yesterday() {
    let d = moment().add(-1, 'day');

    return [d.format(this.format + this.dayS), d.format(this.format + this.dayE)]
  }

  currentWeek() {
    let d = new Date();
    let now = d.getTime();
    let day = d.getDay();
    let lt = 24 * 3600 * 1000;
    let dd = now - (day - 1) * lt;
    let de = now + (7 - day) * lt;
    let dst = new Date(dd);
    let det = new Date(de);

    let st = [
      dst.getFullYear(),
      this.padZero(dst.getMonth() + 1),
      this.padZero(dst.getDate())
    ].join('/');

    let et = [
      det.getFullYear(),
      this.padZero(det.getMonth() + 1),
      this.padZero(det.getDate())
    ].join('/');
    return [st, et];
  }

  lastWeek() {
    let d = new Date();
    let now = d.getTime();
    let day = d.getDay();
    let lt = 24 * 3600 * 1000;
    let n = lt * 7 * -1
    let dd = now - (day - 1) * lt + n;
    let de = now + (7 - day) * lt + n;
    let dst = new Date(dd);
    let det = new Date(de);

    let st = [
      dst.getFullYear(),
      this.padZero(dst.getMonth() + 1),
      this.padZero(dst.getDate())
    ].join('/');

    let et = [
      det.getFullYear(),
      this.padZero(det.getMonth() + 1),
      this.padZero(det.getDate())
    ].join('/');
    return [st, et];
  }

  currentMonth() {
    let now = new Date();
    let st = new Date(now.getFullYear(), now.getMonth(), 1);
    let et = new Date(now.getFullYear(), now.getMonth() + 1, 0);

    return [moment(st).format(this.format), moment(et).format(this.format)];
  }

  lastMonth() {
    let now = new Date();
    let st = new Date(now.getFullYear(), now.getMonth() - 1, 1);
    let et = new Date(now.getFullYear(), now.getMonth(), 0);

    return [moment(st).format(this.format), moment(et).format(this.format)];
  }

  currentYear() {
    let now = new Date();
    let st = new Date(now.getFullYear(), 0, 1);
    let et = new Date(now.getFullYear(), 12, 0);

    return [moment(st).format(this.format), moment(et).format(this.format)];
  }

  lastYear() {
    let now = new Date();
    let st = new Date(now.getFullYear() - 1, 0, 1);
    let et = new Date(now.getFullYear() - 1, 12, 0);

    return [moment(st).format(this.format), moment(et).format(this.format)];
  }


  padZero(number) {
    if (isNaN(number)) {
      throw new Error('number is NaN.');
    }
    return parseInt(number) > 9 ? number : ['0', number].join('');
  }
}



/**
 *
 * @type {{d0m: string, d0sT: string, eh: string, dh: string, d0s: string, em: string, dm: string, em0: string, e0s: string, d08: string, es: string, ds: string, d080s: string}}
 */
const FORMAT_TYPE = {
  "ds": "YYYY-MM-DD HH:mm:ss",
  "dm": "YYYY-MM-DD HH:mm",
  "d0m": "YYYY-MM-DD HH:00",
  "d08": "YYYY-MM-DD 08:00",
  "d080s": "YYYY-MM-DD 08:00:00",
  "dh": "YYYY-MM-DD HH",
  "dd": "YYYY-MM-DD",
  "d0s": "YYYY-MM-DD HH:mm:00",
  "d0sT": "YYYY-MM-DDTHH:mm:00",
  "es": "YYYYMMDDHHmmss",
  "eh": "YYYYMMDDHH",
  "em": "YYYYMMDDHHmm",
  "em0": "YYYYMMDDHHmm",
  "e0s": "YYYYMMDDHHmm00"
}

/**
 * @param {
 * 'ds' |
 * 'dm' |
 * 'd0m' |
 * 'd08' |
 * 'dh' |
 * 'dd' |
 * 'd0s' |
 * 'd0sT' |
 * 'es' |
 * 'eh' |
 * 'em' |
 * 'em0' |
 * 'e0s'
 * } type
 *
 */
export const dateFormat = (type) => {
  if (FORMAT_TYPE[type]) {
    return FORMAT_TYPE[type]
  }
  throw ("时间格式不存在!")
}
